layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'dianzi_list',
		form_filter = 'dianzi_search_form';
	var where_json = {
		_o: 'id',
		_b: 'desc'
	};
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: dianzi_list_url,
		method: 'post',
		height: 'full-200',
		cols: [dianzi_cols],
		where: {
		},
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 20,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: false,
		defaultToolbar: [],
		initSort: {
			field: 'id',
			type: 'desc'
		}
	});

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	
	$('#dianzi_search_btn').on('click', function() {
		layer.load();
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	
	
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		where_json[name] = value;
		
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		where_json[name] = value;
	});

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var id = data.id;
		switch (e) {
			case 'view':
				layer.open({
					type: 2,
					anim: -1,
					title: '查看订单',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: view_order_url + '?order_no=' + data.order_no
				})
				break;
			case 'history':
				layer.open({
					type: 2,
					anim: -1,
					title: '手机号：'+data.phone+'：历史回款时长',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '600px',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: history_url + '?phone=' + data.phone
				})
				break;
			case 'refuse':
				layer.prompt({
					formType: 0,
					title: '拒绝原因',
					area: ['600px', '350px'],
					value: '无'
				}, (value, index, elem) => {
					send_info(dianzi_refuse_url,{reason:value,id:id},(data)=>{
						layer.closeAll();
						if (data.code == 0) {
							layer.msg('操作成功', {
								icon: 1
							});
							table.reload(table_filter);
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'pass':
				layer.confirm('确定审核通过吗？', function(index) {
					send_info(dianzi_pass_url, {
						id: id
					}, function(data) {
						if (data.code == 0) {
							layer.msg('操作成功', {
								icon: 1
							});
							table.reload(table_filter);
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				});
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(site_delete_url, {
						site_id: site_id
					}, function(data) {
						if (data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_site_list"]').length > 0) {
								table.reload('recycle_site_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			default:
				break;
		}
	})
	
	// 订单时间
	var order_time_start = laydate.render({
		elem: 'input[name="order_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				order_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['create_time_start'] = value
		}
	});
	var order_time_end = laydate.render({
		elem: 'input[name="order_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				order_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['order_time_end'] = value
		}
	});
	
	// 申请时间
	var apply_time_start = laydate.render({
		elem: 'input[name="apply_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				apply_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['apply_time_start'] = value
		}
	});
	var apply_time_end = laydate.render({
		elem: 'input[name="apply_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				apply_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['apply_time_end'] = value
		}
	});
	
	// 审核时间
	var audit_time_start = laydate.render({
		elem: 'input[name="audit_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				audit_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['audit_time_start'] = value
		}
	});
	var audit_time_end = laydate.render({
		elem: 'input[name="audit_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				audit_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['audit_time_end'] = value
		}
	});
})